package com.itheima.io.字符流;

import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;

public class CharDtreamDemo4_2 {
    public static void main(String[] args) throws IOException {
        //创建输入流对象
        FileReader fr = new FileReader("day12_IO流_字符流\\a.txt");
        //创建用户集合
        HashMap<String,String> hm = new HashMap<>();
        getMsg(fr, hm);

        //创建键盘录入对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = sc.nextLine();
        System.out.println("请输入密码:");
        String password = sc.nextLine();

        if (hm.containsKey(username)) {
            if (hm.get(username).equals(password)){
                System.out.println("登陆成功");
            }else
                System.out.println("密码错误");
        }else
            System.out.println("用户名错误");

    }

    public static void getMsg(FileReader fr, HashMap<String, String> hm) throws IOException {
        //定义字符串临时储存用户信息
        StringBuilder stringBuilder = new StringBuilder();
        //获取用户信息
        char[] chars = new char[1024];
        int len;
        while ((len = fr.read(chars)) != -1){
            stringBuilder.append(chars,0,len);
        }
        fr.close();
        String allMsg = stringBuilder.toString();
        if (allMsg.isEmpty()||allMsg==null)
            return;
        String[] users = allMsg.split(System.lineSeparator());
        for (String user : users) {
            String[] split = user.split("=");
            hm.put(split[0],split[1]);
        }
    }
}
